package com.kifas.service.dataaccess;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.kifas.entity.BarangEntity;

@Repository
public class BarangDao {

	@Autowired
	private SessionFactory sessionFactory;

	private static final String ALL_BARANG = "FROM BarangEntity be ";
	private static final String BARANG_BY_ID = ALL_BARANG
			+ " where be.idBarang = :idBarang";

	@SuppressWarnings("unchecked")
	public List<BarangEntity> getBarangs() {
		return sessionFactory.getCurrentSession().createQuery(ALL_BARANG)
				.list();
	}

	public BarangEntity getBarangById(Long id) {
		return (BarangEntity) sessionFactory.getCurrentSession()
				.createQuery(BARANG_BY_ID).setLong("idBarang", id)
				.uniqueResult();
	}

	public void insert(BarangEntity barang) throws Exception {
		sessionFactory.getCurrentSession().save(barang);
	}
	
	public void update(BarangEntity barang) throws Exception {
		sessionFactory.getCurrentSession().update(barang);
	}
	

}
